package war2100.entity.npcs;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import org.newdawn.slick.geom.Vector2f;
import war2100.GameConstants;
import war2100.Prototypes;
import war2100.game.Player;
import war2100.game.helper.Vector2Helper;
import war2100.game.items.ammo.Ammo_Pistol;
import war2100.game.states.GameplayState;

public class PlayerCharacter extends Npc {
	
	private Player player;

	public PlayerCharacter(float x, float y, Player player, GameplayState gs, Prototypes prototypes) {
		super(x, y, prototypes.getSlickImage(Prototypes.NPC_PLAYER_PROTOTYP), gs, prototypes);
		this.player = player;
	}

	public void update(GameContainer gc, int delta) throws SlickException {
		float changeX = 0, changeY = 0;
		float deltaF = ((float) delta) / 1000f;
		
		if (player != null && player.isLocalPlayer()) {
			
			float angle = Vector2Helper.getTargetAngle(getPosition(gc), new Vector2f(gc.getInput().getMouseX(), gc.getInput().getMouseY()));
			this.setAngle(angle);
			
			Vector2f dir = new Vector2f(gc.getInput().getMouseX(), gc.getInput().getMouseY()).sub(getPosition(gc)).normalise();
			//Vector2f left = dir.getPerpendicular().normalise();
			
			if (gc.getInput().isKeyDown(player.getKey_up())) {
				//changeX += dir.getX() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				//changeY += dir.getY() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				changeY -= GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
			}
			if (gc.getInput().isKeyDown(player.getKey_down())) {
				//changeX -= dir.getX() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				//changeY -= dir.getY() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				changeY += GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
			}
			if (gc.getInput().isKeyDown(player.getKey_left())) {
				//changeX -= left.getX() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				//changeY -= left.getY() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				changeX -= GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
			}
			if (gc.getInput().isKeyDown(player.getKey_right())) {
				//changeX += left.getX() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				//changeY += left.getY() * GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
				changeX += GameConstants.PLAYER_MOVEMENT_SPEED * deltaF;
			}
			
			if (gc.getInput().isKeyDown(player.getKey_shot())) {
				//gameState.getShoots().add(new Ammo_Pistol(gameState, prototypes, getPosition(gc), dir));
				getCurrentWeapon().shoot(getPosition(gc), dir);
			}
			
			
			
			
			
			float newX = this.x + changeX;
			float newY = this.y + changeY;
			
			if ((Math.floor(x) == Math.floor(newX) && Math.floor(newY) == Math.floor(y)) 
					|| (map.getField((int) newX, (int) newY).isWalkable() 
					 && map.getField((int) newX + 1, (int) newY + 1).isWalkable())) {
				this.x = newX;
				this.y = newY;
			}
		}
	}

	@Override
	public void render(Graphics g, GameContainer gc) {
		super.render(g, gc);
		
	}

}
